package com.ruoyi.system.service.impl;


import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysPost;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.entity.SysPositionTemp;
import com.ruoyi.compensationManagement.domain.CompensationSalarySetting;
import com.ruoyi.compensationManagement.domain.PositionSettings;
import com.ruoyi.system.mapper.SysCommonMapper;
import com.ruoyi.system.service.ISysCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 系统共用获取 服务层
 *
 * @author zy
 */
@Service
public class SysCommonServiceImpl implements ISysCommonService {
    @Autowired
    private SysCommonMapper sysCommonMapper;

    @Override
    public String getDeptName(Long deptId) {
        return sysCommonMapper.getDeptName(deptId);
    }

    @Override
    public String getUserName(Long userId) {
        return sysCommonMapper.getUserName(userId);
    }

    @Override
    public Long getDeptIdByUserId(Long userId) {
        return sysCommonMapper.getDeptIdByUserId(userId);
    }

    @Override
    public SysUser getSysUserByUserId(Long userId) {
        return sysCommonMapper.getSysUserByUserId(userId);
    }

    @Override
    public String getDeptNameByUserId(Long userId) {
        return sysCommonMapper.getDeptNameByUserId(userId);
    }

    @Override
    public String getPersonnelCategoryByUserId(Long userId) {
        return sysCommonMapper.getPersonnelCategoryByUserId(userId);
    }

    @Override
    public Date getBirthdDay(Long userId) {
        return sysCommonMapper.getBirthdDay(userId);
    }

    @Override
    public String getUserPositionByUserId(Long userId) {
        return sysCommonMapper.getUserPositionByUserId(userId);
    }

    @Override
    public List<String> getRoleName() {
        return sysCommonMapper.getRoleName();
    }

    @Override
    public List<String> getPostName() {
        return sysCommonMapper.getPostName();
    }

    @Override
    public List<Map<String, Object>> getSealMsterList() {
        return sysCommonMapper.getSealMsterList();
    }

    @Override
    public Date getEmploymentConfirmationTime(Long userId) {
        return sysCommonMapper.getEmploymentConfirmationTime(userId);
    }

    @Override
    public Date getContractEndTime(Long userId) {
        return sysCommonMapper.getContractEndTime(userId);
    }

    @Override
    public List<Map<String, Object>> getSealDeptMaster(Long deptId) {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Map<String, Object>> sealDeptMaster = sysCommonMapper.getSealDeptMaster();
        for (Map<String, Object> stringObjectMap : sealDeptMaster) {
            if (stringObjectMap.get("dept_id").equals(deptId)) {
                list.add(stringObjectMap);
            }
        }
        return list;
    }

    @Override
    public List<SysPositionTemp> selectDeptList() {
        return sysCommonMapper.getSysPositionTempList();
    }

    /**
     * 构建前端所需要树结构
     *
     * @param positions 岗位列表
     * @return 树结构列表
     */
    @Override
    public List<SysPositionTemp> buildPositionTree(List<SysPositionTemp> positions) {
        List<SysPositionTemp> returnList = new ArrayList<SysPositionTemp>();
        List<Long> tempList = new ArrayList<Long>();
        for (SysPositionTemp position : positions) {
            tempList.add(position.getPositionId());
        }
        for (SysPositionTemp position : positions) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(position.getParentId())) {
                recursionFn(positions, position);
                returnList.add(position);
            }
        }
        if (returnList.isEmpty()) {
            returnList = positions;
        }
        return returnList;

    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param positions 岗位列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildPositionTreeSelect(List<SysPositionTemp> positions) {
        List<SysPositionTemp> positionTrees = buildPositionTree(positions);
        return positionTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysPositionTemp> list, SysPositionTemp t) {
        // 得到子节点列表
        List<SysPositionTemp> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysPositionTemp tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysPositionTemp> getChildList(List<SysPositionTemp> list, SysPositionTemp t) {
        List<SysPositionTemp> tlist = new ArrayList<SysPositionTemp>();
        Iterator<SysPositionTemp> it = list.iterator();
        while (it.hasNext()) {
            SysPositionTemp n = (SysPositionTemp) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getPositionId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysPositionTemp> list, SysPositionTemp t) {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 查岗位类型父节点
     *
     * @return
     */
    @Override
    public List<SysPositionTemp> selectPositionType() {
        return sysCommonMapper.selectPositionType();
    }

    @Override
    public List<SysPositionTemp> getPositionLevelOption(Long positionIds) {
        return sysCommonMapper.getPositionLevelOption(positionIds);
    }

    @Override
    public SysPositionTemp selectPositionByUserId(Long userId) {
        return sysCommonMapper.selectPositionByUserId(userId);
    }

    @Override
    public SysPositionTemp selectPositionLevelByUserId(Long userId) {
        return sysCommonMapper.selectPositionLevelByUserId(userId);
    }

    @Override
    public List<SysPositionTemp> getPositionLevelOptions() {
        return sysCommonMapper.getPositionLevelOptions();
    }

    /**
     * 根据用户id获取岗位等级薪资信息
     *
     * @param userId
     * @return PositionSettings
     */
    @Override
    public PositionSettings getPositionSettingsByUserId(Long userId) {
        return sysCommonMapper.getPositionSettingsByUserId(userId);
    }

    /**
     * 根据user表中的薪级id获取薪级信息
     *
     * @param userId
     * @return CompensationSalarySetting
     */
    @Override
    public CompensationSalarySetting getCompensationSalarySettingByPayId(Long userId) {
        return sysCommonMapper.getCompensationSalarySettingByPayId(userId);
    }

    @Override
    public String getNickNameByJobNumber(String jobNumber) {
        return sysCommonMapper.getNickNameByJobNumber(jobNumber);
    }



    @Override
    public SysPost selectPostByUserId(Long userId) {
        return sysCommonMapper.selectPostByUserId(userId);
    }

    @Override
    public List<SysPost> selectPostType() {
        return sysCommonMapper.selectPostType();
    }
}
